package com.um.springboot.starter.controller;

import cn.hutool.core.util.StrUtil;
import com.um.springboot.starter.code.CommonCode;
import com.um.springboot.starter.helper.SessionHelper;
import com.um.springboot.starter.properties.HshProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 邓南辉
 * @date 2020/2/11
 * @copyright 广州优匠科技有限公司
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/ue_file")
public class UEFileController {

    @Autowired
    HshProperties hshProperties;

    @Autowired
    SessionHelper sessionHelper;

    @RequestMapping(value = "/upload/image", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
      @ResponseBody
      public Map<String,Object> save(HttpServletRequest req) {

        Map<String, Object> rs = new HashMap<String, Object>();
        if(!hshProperties.getFile().isAllowUEditorUpload()){
            rs.put("state", CommonCode.NotOpen.getMsg()); //在此处写上错误提示信息，这样当错误的时候就会显示此信息
            rs.put("url", "");
            rs.put("title", "");
            rs.put("original", "");
            return rs;
        }
        if (!hshProperties.getFile().isAllowAnonymousUpload() && StrUtil.isBlank(sessionHelper.getSessionUserId())) {
            rs.put("state", CommonCode.LoginExpired.getMsg());
            rs.put("url", "");
            rs.put("title", "");
            rs.put("original", "");
            return rs;
        }

        //Map<String, Object> rs = new HashMap<String, Object>();
        MultipartHttpServletRequest mReq = null;
        MultipartFile file = null;
        String fileName = "";
        // 原始文件名  UEDITOR创建页面元素时的alt和title属性
        String originalFileName = "";
        try {
            mReq = (MultipartHttpServletRequest) req;
            // 从config.json中取得上传文件的ID
            file = mReq.getFile("upfile");

            if (!file.isEmpty()) {
                // 取得文件的原始文件名称
                fileName = file.getOriginalFilename();
                originalFileName = fileName;

                String ext = (FilenameUtils.getExtension(file.getOriginalFilename())).toLowerCase();
                String storePath = "";
                if ("jpg".equals(ext) || "png".equals(ext) || "jpeg".equals(ext) || "bmp".equals(ext)) {
                    storePath = "upload/image/";
                } else {
                    storePath = "upload/video/";
                }
                //将图片和视频保存在本地服务器
                String pathRoot = req.getSession().getServletContext().getRealPath("");
                String path = pathRoot + "/" + storePath;

                file.transferTo(new File(path + fileName));
                //String doMain = readProperties.getFileDomain();
                String httpImgPath =  "/"+storePath + fileName;

                rs.put("state", "SUCCESS");// UEDITOR的规则:不为SUCCESS则显示state的内容
                rs.put("url", httpImgPath);     //能访问到你现在图片的路径
                rs.put("title", originalFileName);
                rs.put("original", originalFileName);
            }


        } catch (Exception e) {
            log.error("", e);
            rs.put("state", "文件上传失败!"); //在此处写上错误提示信息，这样当错误的时候就会显示此信息
            rs.put("url", "");
            rs.put("title", "");
            rs.put("original", "");
        }
        return rs;
    }
}
